home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / C / Snippets / SimpleParseUtils 1.0.3 / SimpleParseUtils.cp next >
Encoding:
Text File  |  1996-01-14  |  7.3 KB  |  351 lines  |  [TEXT/CWIE]

  1. /*
  2.     File:        SimpleParseUtils.cp
  3.     
  4.     Contains:    Utilities for useful for very simple
  5.                 parsing tasks.
  6.     
  7.     Version:    1.0.2 (for System 7.x)
  8.     
  9.                 1.0.1    -    2 December 1995  - Added StringHasExtension.
  10.                 1.0.2    -    7 January 1996  - Added PtrAppendStr and CollapsePtrWhiteSpace.
  11.                 1.0.3    -    13 January 1996  - Added AddDotExtension.
  12.     
  13.     Copyright:    ©1995 Chris K. Thomas.  All Rights Reserved.
  14. */
  15.  
  16. #ifndef Assert_
  17. #define Assert_(x) if(!x) DebugStr("\p AssertionFailed: " #x);
  18. #define ThrowIfNULL_(x)
  19. #define ThrowIfMemFail_(x)
  20. #endif
  21.  
  22. #include "SimpleParseUtils.h"
  23.  
  24. // * if inMem points to the same string as inString, true
  25. Boolean StrEquals(const unsigned char *inString, char *inMem)
  26. {
  27.     Boolean out = true;
  28.     
  29.     for(long i = 1; i <= inString[0]; i++)
  30.     {
  31.         if(*(inString + i) != *(unsigned char *)(inMem + i))
  32.         {
  33.             out = false;
  34.             break;
  35.         }
  36.     }
  37.     
  38.     return out;
  39. }
  40.  
  41. // * if inMem points to a the same string as inString followed by whitespace, true
  42. Boolean StrEqualsToken(const unsigned char *inString, char *inMem)
  43. {
  44.     Boolean out = true;
  45.     
  46.     for(long i = 1; i <= inString[0]; i++)
  47.     {
  48.         if(*(inString + i) != *(unsigned char *)(inMem + i))
  49.         {
  50.             out = false;
  51.             break;
  52.         }
  53.     }
  54.     
  55.     // * if the character after the string isn’t whitespace, false
  56.     if(out)
  57.         out = StrEqualsChars(whiteSpaces, (char *)&inMem[inString[0] + 1]);
  58.     
  59.     return out;
  60. }
  61.  
  62. // * if inMem points to a the same string as inString followed by whitespace, true
  63. Boolean PtrEqualsToken(const Ptr inString, char *inMem)
  64. {
  65.     Assert_(inString);
  66.     Boolean out = true;
  67.     
  68.     long strSize = GetPtrSize(inString);
  69.     
  70.     for(long i = 0; i < strSize; i++)
  71.     {
  72.         if(*(inString + i) != *(unsigned char *)(inMem + i))
  73.         {
  74.             out = false;
  75.             break;
  76.         }
  77.     }
  78.     
  79.     // * if the character after the string isn’t wordbreak, false
  80.     if(out)
  81.         out = StrEqualsChars(wordBreaks, (char *)&inMem[ strSize ]);
  82.     
  83.     return out;
  84. }
  85.  
  86. // * if inMem points to a char which is in inTryChars, true
  87. Boolean StrEqualsChars(const unsigned char *inTryChars, char *inMem)
  88. {
  89.     Assert_(inTryChars);
  90.     Assert_(inMem);
  91.     
  92.     Boolean out = false;
  93.     
  94.     for(long i = 1; i <= inTryChars[0];i++)
  95.     {
  96.         if(*(inTryChars + i) == *(unsigned char *)inMem)
  97.         {
  98.             out = true;
  99.             break;
  100.         }
  101.     }
  102.     
  103.     return out;
  104. }
  105.  
  106. // * count whitespace chars
  107. long CountWhiteSpace(char *inMem, long inLength)
  108. {
  109.     Assert_(inMem);
  110.     
  111.     long out = 0;
  112.     
  113.     while(StrEqualsChars(whiteSpaces, inMem + out)
  114.             && (out < inLength))
  115.     {
  116.         out++;
  117.     }
  118.     
  119.     return out;
  120. }
  121.  
  122. // * return a string containing the next token
  123. StringPtr GetTokenString(char *inMem, long inLength)
  124. {
  125.     Assert_(inMem);
  126.     
  127.     StringPtr        curString;
  128.     long            wordLength = 0;
  129.     long            startIndex;
  130.     
  131.     startIndex = CountWhiteSpace(inMem, inLength);
  132.     
  133.     while(!StrEqualsChars(wordBreaks, (inMem + startIndex + wordLength)))
  134.         wordLength++;
  135.     
  136.     curString = (StringPtr)NewPtr(wordLength + 1);
  137.     ThrowIfNULL_(curString);
  138.     
  139.     BlockMoveData(inMem + startIndex, &curString[1], wordLength);
  140.     curString[0] = wordLength;
  141.     
  142.     return curString;
  143. }
  144.  
  145. // * return a length-unlimited string containing the next token
  146. Ptr GetTokenPtr(char *inMem, long inLength)
  147. {
  148.     Assert_(inMem);
  149.     
  150.     Ptr                curString;
  151.     long            wordLength = 0;
  152.     long            startIndex;
  153.     
  154.     startIndex = CountWhiteSpace(inMem, inLength);
  155.     
  156.     while(!StrEqualsChars(wordBreaks, (inMem + startIndex + wordLength)))
  157.         wordLength++;
  158.     
  159.     curString = NewPtr(wordLength);
  160.     ThrowIfMemFail_(curString);
  161.     
  162.     BlockMoveData(inMem + startIndex, &curString[0], wordLength);
  163.     
  164.     return curString;
  165. }
  166.  
  167. Ptr GetDelimitedToken(const unsigned char *inDelimiter, char *inMem, long inLength)
  168. {
  169.     Assert_(inMem);
  170.     
  171.     Ptr                curString;
  172.     long            wordLength = 0;
  173.     long            startIndex;
  174.     
  175.     startIndex = CountWhiteSpace(inMem, inLength);
  176.     
  177.     while(!StrEqualsChars(inDelimiter, (inMem + startIndex + wordLength)))
  178.         wordLength++;
  179.     
  180.     curString = NewPtr(wordLength);
  181.     ThrowIfMemFail_(curString);
  182.     
  183.     BlockMoveData(inMem + startIndex, &curString[0], wordLength);
  184.     
  185.     return curString;
  186. }
  187.  
  188. // * return the char in inString actually found
  189. short FindSelectChar(const unsigned char *inString, char *inMem, long inLength)
  190. {
  191.     long    i = 0;
  192.     short    out = 0;
  193.     
  194.     while(i < inLength)
  195.     {
  196.         
  197.         // * compare the current char in inMem with each char in inString
  198.         for(long charIndex = 1; charIndex <= inString[0]; charIndex++)
  199.         {
  200.             if(inMem[i] == inString[charIndex])
  201.             {
  202.                 out = inString[charIndex];
  203.                 
  204.                 // * break out of the outer loop
  205.                 // * without requiring a doneFlag
  206.                 i = inLength + 5;    // * could be any value
  207.                 break;
  208.             }
  209.         }
  210.         
  211.         i++;
  212.     }
  213.     
  214.     return out;
  215. }
  216.  
  217. // * get all text from inMem to end of line
  218. Ptr    GetEntireLine(char *inMem, long inLength)
  219. {
  220.     long    lineLength = 0;
  221.     Ptr        out = NULL;
  222.     
  223.     while(!StrEqualsChars(endLines, &inMem[lineLength]) && lineLength < inLength)
  224.         lineLength++;
  225.     
  226.     out = NewPtr(lineLength);
  227.     ThrowIfMemFail_(out);
  228.     
  229.     BlockMoveData(&inMem[0], out, lineLength);
  230.     
  231.     return out;
  232. }
  233.  
  234. // * for checking dot extensions (.c .h .pas .r, etc)
  235. Boolean
  236. StringHasExtension(const unsigned char * inExtension, const unsigned char *inString)
  237. {
  238.     Boolean    out = true;
  239.     long    extensionIndex = inExtension[0];
  240.     long    stringIndex = inString[0];
  241.     
  242.     //
  243.     // if the extension is bigger than the string
  244.     // the string can’t have the extension
  245.     //
  246.     if(inExtension[0] <= inString[0])
  247.     {
  248.         for(long index = inExtension[0]; extensionIndex > 0; )
  249.         {
  250.             if(inString[stringIndex] != inExtension[extensionIndex])
  251.             {
  252.                 out = false;
  253.                 break;
  254.             }
  255.             
  256.             extensionIndex--;
  257.             stringIndex--;
  258.         }
  259.     }
  260.     else
  261.     {
  262.         out = false;
  263.     }
  264.     
  265.     return out;
  266. }
  267.  
  268. // * append a pstring to the given Ptr
  269. void
  270. PtrAppendStr(Ptr inDestPtr, unsigned char *inStringToAppend)
  271. {
  272.     long formerLength = GetPtrSize(inDestPtr);
  273.     
  274.     SetPtrSize(inDestPtr, formerLength + *inStringToAppend);
  275.     if(MemError() != noErr)
  276.         throw MemError();
  277.     
  278.     BlockMoveData(&inStringToAppend[1], &inDestPtr[formerLength], *inStringToAppend);
  279. }
  280.  
  281. // * strip all but one space from contiguous runs of whitespace; can shrink inDestPtr
  282. void CollapsePtrWhiteSpace(Ptr inDestPtr)
  283. {
  284.     long size = GetPtrSize(inDestPtr);
  285.     long currentSpace;
  286.  
  287.     for(long index = 0; index < size; index++)
  288.     {
  289.         currentSpace = CountWhiteSpace(&inDestPtr[index], size);
  290.         
  291.         if(currentSpace > 0)
  292.         {
  293.             // shift the bytes down to where we only have one space
  294.             BlockMoveData(&inDestPtr[index + currentSpace], &inDestPtr[index + 1], size - (index + currentSpace - 1));
  295.             
  296.             // make it a real space, not a tab or CR/LF
  297.             inDestPtr[index] = ' ';
  298.             
  299.             // shrink ptr size
  300.             size -= (currentSpace - 1);
  301.             SetPtrSize(inDestPtr, size);
  302.             if(MemError() != noErr)
  303.                 throw MemError();
  304.         }
  305.     }
  306. }
  307.  
  308. // * look for inFindChar until either we hit a char in inUntil or index >= inLength
  309. Boolean FindCharUntil(short inFindChar, const unsigned char *inUntil, char *inMem, long inLength)
  310. {
  311.     Boolean out = false; //pessimism
  312.     
  313.     
  314. //    DebugStr((unsigned char *)(inMem - 1));
  315.     for(long index = 0; index < inLength; index++)
  316.     {
  317.         if(inFindChar == inMem[index])
  318.         {
  319.             // success!
  320.             out = true;
  321.             break;
  322.         }
  323.         else if(StrEqualsChars(inUntil, &inMem[index]))
  324.         {
  325.             // found an "until" char- failure
  326.             break;
  327.         }
  328.         
  329.     }
  330.     
  331.     return out;
  332. }
  333.  
  334. // * add a .dot extension to a Mac filename (yuk!)
  335. void AddDotExtension(const Str255 inString, const Str255 inDot, Str255 outString)
  336. {
  337.     if(inString[0] + inDot[0] >= 31)
  338.     {
  339.         BlockMoveData(&inString[1], &outString[1], 31 - inDot[0]);
  340.         BlockMoveData(&inDot[1], &outString[31 - inDot[0]], inDot[0]);
  341.         outString[0] = 31;
  342.     }
  343.     else
  344.     {
  345.         BlockMoveData(&inString[1], &outString[1], inString[0]);
  346.         BlockMoveData(&inDot[1], &outString[ inString[0] + 1], inDot[0]);
  347.         outString[0] = inDot[0] + inString[0];
  348.     }
  349. }
  350.  
  351.